<template lang="">
    <div>
        <el-dialog :title="title" :visible.sync="open" width="80%">
            <el-row :style="{ marginBottom: '10px' }">
                <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">创建临时指令任务</el-button>
                <el-button type="success" icon="el-icon-plus" size="mini" @click="handleExecute">下发子任务</el-button>
                <el-button type="success" icon="el-icon-refresh" size="mini" @click="handleRefresh" style="float: right">手动刷新子任务</el-button>
            </el-row>

            <el-table :data="list">
                <el-table-column prop="userName" label="执行人" width="180"></el-table-column>
                <el-table-column prop="groupName" label="所属分组" width="180"></el-table-column>
                <el-table-column prop="deptName" label="所属部门" width="180"></el-table-column>
                <el-table-column label="任务内容" align="center" prop="taskContent" />
                <el-table-column label="辅助信息" align="center" prop="helpContent" />
                <el-table-column label="任务状态" align="center" prop="status">
                    <template slot-scope="scope">
                        <el-tag type="warning" v-if="scope.row.status == 0" size="mini">待下发</el-tag>
                        <el-tag v-if="scope.row.status == 1" size="mini">执行中</el-tag>
                        <el-tag type="success" v-if="scope.row.status == 2" size="mini">已结束</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250">
                    <template slot-scope="scope">
                        <el-button @click="handleDrillTaskEdit(scope.row)" v-if="scope.row.status == 0" type="text" size="small">编辑</el-button>
                        <el-button @click="handleDrillTaskDel(scope.row)" v-if="scope.row.status == 0" type="text" size="small">删除</el-button>
                        <el-button @click="handleTaskDetail(scope.row)" type="text" size="small">任务详情</el-button>
                        <el-button type="text" size="mini" v-if="scope.row.userName != null" @click="chat(scope.row)">任务沟通</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <el-row :style="{ textAlign: 'center', marginTop: '15px' }">
                <el-button type="primary" icon="el-icon-check" size="mini" @click="handleComplete">完成任务</el-button>
            </el-row>
        </el-dialog>
        <drill-task-edit ref="drillTaskEdit" @handleOk="onDrillTaskSave" :needMsg="false" />
        <ChatIM ref="chatIM" />
        <TaskSonDetail ref="taskDetailRef" />
    </div>
</template>
<script>
/**
 * 查询演练子任务列表弹窗
 * 用于查询一级任务下属的所有二级子任务
 * 功能用于任务下发或者添加临时任务
 */
import dialogMixin from "@/mixins/dialogMixin";
import * as DrillApi from "@/api/drill/drill";
import DrillTaskEdit from "./DrillTaskEdit.vue";
import { getChildTaskList, drillTaskSet, startDrillSonTask, completeTask } from "@/api/drill/task";
import chatMixin from "@/mixins/chatMixin";
import ChatIM from "@/components/ChatIM/task.vue";
import TaskSonDetail from "./DrillSonTaskDetail.vue";

export default {
    name: "DrillSonTasks",
    mixins: [dialogMixin, chatMixin],
    components: { DrillTaskEdit, ChatIM, TaskSonDetail },
    data() {
        return {
            title: "演练子任务列表",
            list: [],
            taskId: 0,
            handleId: 0,
            levelId: 0,
            groups: [],
        };
    },
    provide() {
        return {
            groups: () => this.groups,
        };
    },
    mounted() {},
    methods: {
        showList(taskId, handleId, levelId, drillPlanId) {
            this.taskId = taskId;
            this.handleId = handleId;
            this.levelId = levelId;
            this.getDrillPlan(drillPlanId);
            this.handleRefresh();
            this.show();
        },
        getDrillPlan(drillPlanId) {
            DrillApi.getDrillPlanInfo(drillPlanId).then(res => {
                console.log(res);
                this.groups = res.data.drillPractsGroups;
            });
        },
        handleRefresh() {
            getChildTaskList(this.taskId).then(res => {
                this.list = res.data;
            });
        },
        handleAdd() {
            this.$refs.drillTaskEdit.addChildTask(this.taskId, this.levelId);
        },
        handleDrillTaskEdit(row) {
            this.$refs.drillTaskEdit.edit(row);
        },
        onDrillTaskSave(data) {
            drillTaskSet(data).then(res => {
                this.$modal.msgSuccess("演练任务编辑成功");
                this.handleRefresh();
            });
        },
        handleExecute() {
            // 判断子任务是否都有执行人
            if (this.list.some(item => item.receiveUser == null)) {
                this.$modal.msgError("请先为子任务选择执行人员");
                return;
            }
            // 判断是否有未下发的任务
            if (this.list.some(item => item.status != 0)) {
                this.$modal.msg("所有任务均已下发");
                return;
            }
            startDrillSonTask(this.taskId).then(res => {
                if (res.code == 200) {
                    // 开启成功
                    this.$modal.msgSuccess("子任务已经下发");
                    this.handleRefresh();
                } else {
                    // 开启失败
                    this.$modal.msgError("子任务下发失败");
                }
            });
        },
        handleComplete() {
            // 二级保障部门检查所有子任务都完成后，向总指挥提交任务完成
            // 判断子任务是否都执行完成
            if (this.list.some(item => item.status != 2)) {
                this.$modal.msgError("请确保所有任务均已完成");
                return;
            }

            completeTask(this.taskId).then(res => {
                if (res.code == 200) {
                    // 提交成功
                    this.$modal.msgSuccess("任务已完成");
                    this.$emit("handleOk");
                    this.close();
                } else {
                    // 提交失败
                    this.$modal.msgError("任务提交失败");
                }
            });
        },
        chat(row) {
            console.log(row);
            this.initTaskChat("drill", row.planId, row.taskId, row.userName);
        },
        handleTaskDetail(row) {
            this.$refs.taskDetailRef.showDialog(row);
        },
    },
};
</script>
<style lang=""></style>
